package com.woniuxy.student.service.impl;

import com.woniuxy.commons.entity.AnswerSheet;
import com.woniuxy.commons.entity.StudentInfo;
import com.woniuxy.student.dao.AnswerSheetDao;
import com.woniuxy.student.dao.StudentInfoDao;
import com.woniuxy.student.service.AnswerSheetService;
import com.woniuxy.student.service.ReplySheetService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/18 0018-下午 4:22
 */
@Service
public class AnswerSheetServiceImpl implements AnswerSheetService {
    @Resource
    AnswerSheetDao answerSheetDao;
    @Resource
    StudentInfoDao studentInfoDao;
    @Resource
    ReplySheetService replySheetService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    public AnswerSheet getAnswerSheetByUserid(int studentno, int paperid) {
        return answerSheetDao.getAnswerSheetByUserid(studentno, paperid);
    }


    @Override
    @Transactional
    public int insertAnswerSheet(String key) {
        String replies = (String) redisTemplate.opsForValue().get(key);
        System.out.println("replies:"+replies);
        if("-1".equals(replies)){
            return 1; //如果是-1则表明是考生自己手动交卷直接退出；
        }
        String[]infos = key.split(":");
        long paperid = Long.parseLong(infos[1]);
        Integer studentno = Integer.parseInt(infos[2]);
        StudentInfo studentInfo = studentInfoDao.getInfo(studentno);
        String sname = studentInfo.getSname();
        String schoolname = studentInfo.getSchoolInfo().getSname();
        String classname = studentInfo.getTeamInfo().getTname();
        String gradename = studentInfo.getTeamInfo().getGradeInfo().getGradeType();
        AnswerSheet answerSheet  = new AnswerSheet();
        answerSheet.setSheetPaperId(paperid);
        answerSheet.setSheetStudent(sname);
        answerSheet.setSheetStudentNo(studentno);
        answerSheet.setSheetClasses(classname);
        answerSheet.setSheetSchool(schoolname);
        answerSheet.setSheetGrade(gradename);
        answerSheet.setSheetDeal(0);
        int id = 0;
        try{
            answerSheetDao.insertAnswerSheet(answerSheet);
            System.out.println("插入答题表成功");
            id = (int)answerSheet.getSheetId();
            replySheetService.insertReply(id,replies);
            System.out.println("插入答题成功");
            redisTemplate.opsForValue().set(key,"-1",15, TimeUnit.MINUTES);
        }catch (Exception e){
            System.out.println(e);
        }
        return id;
    }

    @Override
    public List<AnswerSheet> getAnswerSheetByStatus(int paperid, int status) {
        return answerSheetDao.getAnswerSheetByStatus(paperid,status);
    }

    /**
     * 将答案保存到redis
     * @param studentno
     * @param paperid
     * @param replies
     * @return
     */
    @Override
    public int updateAnswer(Integer studentno, Integer paperid, String replies) {
        String key = "answer:"+paperid+":"+studentno;
        redisTemplate.opsForValue().set(key,replies);
        return 0;
    }
}
